#include "head_2.h"

extern linknode *H_trf;
extern linknode *E_trf;

extern linknode *H_sql;
extern linknode *E_sql;

extern pthread_mutex_t mutex_analysis;

extern pthread_cond_t cond_analysis;

linknode * node_malloc(){

	linknode *H = NULL;

	if((H = (linknode *)malloc(sizeof(linknode))) == NULL){
		printf("fail to malloc\n");
		return NULL;
	}
	return H;

}

void * list_create() {

	H_sql = node_malloc();
	E_sql = node_malloc();
	H_trf = node_malloc();
	E_trf = node_malloc();
	
	H_sql->next = E_sql;
	H_sql->pro = NULL;
	E_sql->next = NULL;
	E_sql->pro = H_sql;
	
	H_trf->next = E_trf;
	H_trf->pro = NULL;
	E_trf->next = NULL;
	E_trf->pro = H_trf;
	
}

int head_insert(linknode *H, sendEnv value) {

	linknode * p = NULL;

	p = node_malloc();

	H->next->pro = p;
	p->env = value;
	p->next = H->next;
	p->pro = H;
	H->next = p;


	return 0;
}

linknode * take_head(linknode *H) {
	
	linknode * q = NULL;

	if((H->next == E_sql) || (H->next == E_trf)) {

		printf("Linked list is empty!");
		return q;
		
	} else {
	
		q = H->next;
		H->next = H->next->next;
		H->next->pro = H;
		q->next = NULL;
		q->pro = NULL;
		return q;
	
	}
}

linknode * take_end(linknode *E) {
	
	linknode * q = NULL;

	if((E->pro == H_sql) || (E->pro == H_trf)) {

		printf("Linked list is empty!");
		return NULL;
		
	} else {
	
		q = E->pro;
		E->pro = E->pro->pro;
		E->pro->next = E;
		q->next = NULL;
		q->pro = NULL;
		return q;
	
	}
}
